package tool;


import number.Input;
import number.Other;

import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.net.URI;
import java.util.ArrayList;

public class IEControl {

    public void run() {
        Input input = new Input();
        Other other = new Other();
        // 指定要访问的网址
        String url = "https://www.douyin.com/";

        // 检查当前环境是否支持浏览URL
        if(Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
            try {
                // 转换为URI对象
                URI uri = new URI(url);
                // 使用默认浏览器打开网页
                Desktop.getDesktop().browse(uri);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("当前环境不支持打开浏览器");
        }
        try {
            Robot robot = new Robot();
//            打开后识别屏幕，点击朋友按钮
            Thread.sleep(10000);
            System.out.println("识别屏幕，点击私信按钮");
            robot.mouseMove(input.getNumber_1a(), input.getNumber_1b());

            // 模拟鼠标左键点击
            robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);

            Thread.sleep(5000);

            System.out.println("识别屏幕，点击头像");
            // 移动鼠标到指定位置 (x, y)
            robot.mouseMove(input.getNumber_2a(), input.getNumber_2b());

            // 模拟鼠标左键点击
            robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);


            Thread.sleep(5000);


            // 模拟键盘输入1
            robot.keyPress(KeyEvent.VK_1);
            // 模拟键盘输入回车键
            Thread.sleep(1000);
            robot.keyPress(KeyEvent.VK_ENTER);
            // 等待几秒钟，确保消息发送成功
            Thread.sleep(2000);
            ArrayList list = other.getList();
            int num = (int) list.get(0);
            for (int i = 1; i < list.size(); i++) {
                robot.mouseMove( num, (int) list.get(i));
                Thread.sleep(1000);
                // 模拟鼠标左键点击
                robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
                robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
                Thread.sleep(1000);
                // 模拟键盘输入1
                robot.keyPress(KeyEvent.VK_1);
                // 模拟键盘输入回车键
                Thread.sleep(1000);
                robot.keyPress(KeyEvent.VK_ENTER);
            }

            System.out.println("已发送消息");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void stop() {
        System.exit(0);
    }

}
